package com.his.struts.dao.comment;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.his.struts.form.CommentForm;
import com.his.struts.util.DbUtil;
/**
 * 实现点评类
 * @author xiaoweiliang
 *
 */
public class CommentImp implements CommentDao {
	Logger log = Logger.getLogger(CommentImp.class);
	DbUtil db = new DbUtil();
	/**
	 * 添加点评
	 * @param commentForm 点评 bean
	 * @return 是否点评成功
	 */
	public boolean addComment(CommentForm commentForm) {
		boolean ok = false;
		String sql= "insert into comment(userName,doctorName,disease,rx,leechcraft,attitude,info)" +
					" values(?,?,?,?,?,?,?)";
		ArrayList list = new ArrayList();
		list.add(commentForm.getUserName());
		list.add(commentForm.getDoctorName());
		list.add(commentForm.getDisease());
		list.add(commentForm.getRx());
		list.add(commentForm.getLeechcraft());
		list.add(commentForm.getAttitude());
		list.add(commentForm.getInfo());
		int n = db.executeUpdate(sql, list);
		if(n > 0){
			ok = true;
		}
		db.closeAll();
		return ok;
	}
	
	/**
	 * 封装该医生的所有点评
	 * @param rs 医生的所有点评
	 * @return 所有点评
	 */
	public List<CommentForm> getBean(ResultSet rs){
		List<CommentForm> list = new ArrayList();
		try {
			while(rs.next()){
				CommentForm comment = new CommentForm();
				comment.setC_id(rs.getInt("c_id"));
				comment.setUserName(rs.getString("userName"));
				comment.setDoctorName(rs.getString("doctorName"));
				comment.setDisease(rs.getString("disease"));
				comment.setRx(rs.getString("rx"));
				comment.setLeechcraft(rs.getString("leechcraft"));
				comment.setAttitude(rs.getString("attitude"));
				comment.setInfo(rs.getString("info"));
				comment.setSupport(rs.getInt("support"));
				comment.setAgainst(rs.getInt("against"));
				list.add(comment);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return list;
	}
	
	/**
	 * 支持次点评
	 * @param c_id 点评id
	 */
	public void addSupport(int c_id){
		String sql = "update comment set support = support+1 where c_id = ?";
		ArrayList list = new ArrayList();
		list.add(c_id);
		db.executeUpdate(sql, list);
		db.closeAll();
	}
	
	/**
	 * 反对次点评
	 * @param c_id 点评id
	 */
	public void addAgainst(int c_id){
		String sql = "update comment set against = against+1 where c_id = ?";
		ArrayList list = new ArrayList();
		list.add(c_id);
		db.executeUpdate(sql, list);
		db.closeAll();
	}

	/**
	 *  得到此点评支持的数量
	 * @param c_id 点评id
	 * @return 现在支持的数量
	 */
	public int getSupport(int c_id){
		int n = 0;
		String sql = "select support from comment where c_id = ?";
		ArrayList list = new ArrayList();
		list.add(c_id);
		ResultSet rs = db.executeQuery(sql, list);
		try {
			if(rs.next()){
				n = rs.getInt(1);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return n;
	}
	
	/**
	 * 得到反对此点评的数量
	 * @param c_id
	 * @return 现在反对的数量
	 */
	public int getAgainst(int c_id){
		int n = 0;
		String sql = "select against from comment where c_id = ?";
		ArrayList list = new ArrayList();
		list.add(c_id);
		ResultSet rs = db.executeQuery(sql, list);
		try {
			if(rs.next()){
				n = rs.getInt(1);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return n;
	}
	
	/**
	 * 删除点评
	 * @param c_id 点评id
	 */
	public void deleteCommet(int c_id){
		String sql="delete from comment where c_id=?";
		ArrayList list = new ArrayList();
		list.add(c_id);
		db.executeUpdate(sql, list);
		db.closeAll();
	}
	
	/**
	 * 得到医生名字
	 * @param c_id 点评id
	 * @return 医生名字
	 */
	public String getDoctorName(int c_id){
		String doctorName="";
		String sql="select doctorName from comment where c_id=?";
		ArrayList list = new ArrayList();
		list.add(c_id);
		ResultSet rs = db.executeQuery(sql, list);
		try {
			if(rs.next()){
				doctorName = rs.getString(1);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return doctorName;
	}
 
}
